package cn.lg.soar.common.util.current;

import lombok.Getter;
import lombok.Setter;

/**
 * 当前用户工具
 * @author luguoxiang
 * @date 2022/3/29
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Getter
@Setter
public class CurrentUser implements ICurrentUser {

    public final static String USER_KEY = "soar-cur-user";

    /**
     * token标识
     */
    private String tokenId;

    /**
     * token值（并非token）
     */
    private Long tokenValue;

    /**
     * 用户id
     */
    private Long id;

    /**
     * 用户名（账号）
     */
    private String username;

    /**
     * 用户类型
     */
    private Integer userType;

    /**
     *
     */
    private Integer currentTenantId;

    /**
     * 通过 token payload 构造
     * @param payload
     * @return
     */
    public static CurrentUser valueOf(Object[] payload) {
        CurrentUser currentUser = new CurrentUser();
        currentUser.tokenId = payload[0].toString();
        currentUser.tokenValue = Long.parseLong(payload[1].toString());
        currentUser.id = Long.parseLong(payload[3].toString());
        currentUser.username = (String) payload[4];
        currentUser.userType = (Integer) payload[5];
        currentUser.currentTenantId = Integer.parseInt(payload[6].toString());
        return currentUser;
    }
}
